<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Material Design Login Form</title>

    <link rel="stylesheet" href="css/style.css">


</head>

<body onload="onLoad()">
<link href='https://fonts.googleapis.com/css?family=Work+Sans:400,300,700' rel='stylesheet' type='text/css'>

<div class="container">
    <div class="profile profile--open">
        <!--<button class="profile__avatar" id="toggleProfile">
         <img src="http://gravatar.com/avatar/69f34259a9d0b7df04f0b50893c469ed?s=80" alt="Avatar" />
        </button>-->
        <div class="profile__form">
            <div class="profile__fields">

                <div class="field">
                    <input type="text" id="fieldURL" class="input" required readonly placeholder="连接地址"
                           value="localhost:4001"/>
                    <!--<label for="fieldURL" class="label">连接地址</label>-->
                </div>

                <div class="field">
                    <input type="text" id="fieldName" class="input" required placeholder="名字" pattern=.*\S.*/>
                    <!--<label for="fieldName" class="label">名字</label>-->
                </div>
                <div class="profile__footer">
                    <div class="button_commit raised blue">
                        <div class="center" fit>换名字</div>
                        <paper-ripple fit></paper-ripple>
                    </div>
                    <div class="button_commit raised blue">
                        <div onclick="connection()" class="center" fit>上线</div>
                        <paper-ripple fit></paper-ripple>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="js/index.js"></script>

<script type="text/javascript">
    function onLoad() {
        var ck = getCookie('userName');
        console.log(ck);

        if (ck === '') {
            ck = ")&*#()$U#@$";
        }
        document.getElementById('fieldName').value = ck;


    }

    function getCookie(c_name) {
        if (document.cookie.length > 0) {  //先查询cookie是否为空，为空就return ""
            c_start = document.cookie.indexOf(c_name + "=")  //通过String对象的indexOf()来检查这个cookie是否存在，不存在就为 -1
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1  //最后这个+1其实就是表示"="号啦，这样就获取到了cookie值的开始位置
                c_end = document.cookie.indexOf(";", c_start)  //其实我刚看见indexOf()第二个参数的时候猛然有点晕，后来想起来表示指定的开始索引的位置...这句是为了得到值的结束位置。因为需要考虑是否是最后一项，所以通过";"号是否存在来判断
                if (c_end == -1) c_end = document.cookie.length
                return unescape(document.cookie.substring(c_start, c_end))  //通过substring()得到了值。想了解unescape()得先知道escape()是做什么的，都是很重要的基础，想了解的可以搜索下，在文章结尾处也会进行讲解cookie编码细节
            }
        }
        return "";
    }

    function connection() {
        var userName = document.getElementById('fieldName').value;
        document.cookie = "userName=" + userName;

    }


</script>

</body>
</html>
